package actions;

import javax.ejb.EJB;
import java.util.*;
import entities.*;

import javax.ejb.Local;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import utils.*;
import beans.*;

public class HandleRequest extends ActionSupport {

	private static final long serialVersionUID = 4L;

	private int reqId;
	private String response;

	@EJB
	private LoanManager loanMan = new LoanManagerBean();

	public int getReqId() {
		return reqId;
	}

	public void setReqId(int reqId) {
		this.reqId = reqId;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public String execute() {
		Object userIdObj = ActionContext.getContext().getSession()
				.get("userId");
		if (userIdObj == null) {
			this.addActionError("You must login to complete this action");
			return ERROR;
		}
		boolean res;
		boolean isAccept = false;
		if (response.equalsIgnoreCase("Accept")) {
			res = loanMan.approve(reqId);
			isAccept = true;
		} else {
			res = loanMan.deny(reqId);
			isAccept = false;
		}
		if (!res) {
			String action = (isAccept) ? "approving" : "denying";
			ActionContext.getContext().getSession().put("error",
					"An error has occurred while " + action + " your request.");

		} else {
			String action = (isAccept) ? "approved" : "denied";
			ActionContext.getContext().getSession().put("message",
					"Your request has been successfully " + action);
		}
		return SUCCESS;

	}

}